package day8;

public
class TestThread extends Thread {
    public void run(){
        String name=Thread.currentThread().getName();// 获取当前线程的名字
        int sum=0;
        for (int i=0;i<100;i++){
            if (i%2==0) {
                sum+=i;
                System.out.println(name+"偶数和为："+sum);
                try {
                    Thread.sleep(100);
                }catch (InterruptedException e){
                    throw new RuntimeException(e);
                }
            }
        }
    }
    public static
    void main(String[] args) {
        TestThread t=new TestThread();
        t.setName("TestThread-1");
        MyThread myThread=new MyThread();
        Thread t2=new Thread(() -> {
            int sum=0;
            for (int i=0;i<100;i++){
                if (i%2!=0) {
                    sum+=i;
                    System.out.println(Thread.currentThread().getName()+"奇数和为："+sum);
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        });
        myThread.setDaemon(true);
        t2.start();
        t.start();
        myThread.start();
    }
}
class MyThread extends Thread{
    public void run(){
        while (true){
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("我是守护线程："+Thread.currentThread().getName());
        }
    }
}
